<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/11 0011
 * Time: 14:45
 */
namespace Wx\Controller;
use Think\Controller;
class StoreOrderController extends CommonController
{
    public function Index(){
		
		$show_type=0;//默认显示全部
		if($_GET['show_type']>0)$show_type=$_GET['show_type'];
		$this->assign("show_type",$show_type);
		
		//门店拼团订单管理---------------------------------------------start
        $s_id = session("s_id");
        $store = M("shop_store");
        $spellgroup_copy = "xtws_shop_spellgroup_copy"; //拼团副本-数据库表名
        $spellgroup = "xtws_shop_spellgroup";//拼团商品-数据库表名
        $spellgroup_order = "xtws_shop_spellgroup_order";//拼团订单-数据库表名
        //全部商品
        $store_all = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND (s_copy.state = 0 or s_copy.state = 3 or s_copy.state = 4 or s_copy.state = 5 or s_copy.state = 6 ) AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num,s_copy.state as copy_state')
            ->select();
			$shop_spellgroup_order=D('shop_spellgroup_order');
		foreach($store_all as $i=>$one){
            //2018-7-5lxlx 将几份待核销改为几人待核销
			$store_all[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count();   //待核销人数
            $store_all[$i]['need_hx_num']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->sum('num');    //待核销总份数
            $store_all[$i]['already_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=2")->count();        //已核销人数
		}//end foreach
        //门店拼团待成团商品信息
        $store_ds = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 0 AND s_copy.state = 0 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
		//dump($store_ds);
        //门店拼团待通知商品信息
        $store_tz = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 3 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
        //门店拼团待核销商品信息
        $store_hx = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 4 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
		
			$shop_spellgroup_order=D('shop_spellgroup_order');
		foreach($store_hx as $i=>$one){
//			$store_hx[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->sum('num');
            //2018-7-5lxlx 将几份待核销改为几人待核销
            $store_hx[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count();
            $store_hx[$i]['need_hx_num']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->sum('num');    //待核销总份数
		}//end foreach
		//dump($store_hx);
        //门店拼团已核销商品信息
        $store_yhx = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 6 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
        foreach($store_yhx as $i=>$one){
            $store_yhx[$i]['already_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=2")->count();
        }//end foreach
		//dump($store_yhx);
        $this->assign('store_all',$store_all);//
        $this->assign('store_ds',$store_ds);//待成团
        $this->assign('store_tz',$store_tz);
        $this->assign('store_hx',$store_hx);
        $this->assign('store_yhx',$store_yhx);
		//门店拼团订单管理---------------------------------------------end
		
		//乡村拼团订单管理---------------------------------------------start
        $s_id = session("s_id");
        $store = M("shop_store");
        $spellgroup_copy = "xtws_shop_spellgroup_copy"; //拼团副本-数据库表名
        $spellgroup = "xtws_shop_spellgroup";//拼团商品-数据库表名
        $spellgroup_order = "xtws_shop_spellgroup_order";//拼团订单-数据库表名
		//全部订单
        $store_all_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND (s_copy.state = 2 or s_copy.state = 3 or s_copy.state = 4 or s_copy.state = 6) AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num,s_copy.state as copy_state')
            ->select();
			$shop_spellgroup_order=D('shop_spellgroup_order');
		foreach($store_all_xc as $i=>$one){
//			$store_all_xc[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count('num');
            //2018-7-5lxlx 将几份待核销改为几人待核销
            $store_all_xc[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count();    //待核销人数
            $store_all_xc[$i]['need_hx_num']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->sum('num'); //待核销总份数
            $store_all_xc[$i]['already_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=2")->count(); //已核销人数
		}//end foreach
        //农户拼团待收货信息
        $store_ds_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 2 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
		//dump($store_ds);
        //农户拼团待通知商品信息
        $store_tz_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 3 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
        //农户拼团待核销商品信息
        $store_hx_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 4 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
		
			$shop_spellgroup_order=D('shop_spellgroup_order');
		foreach($store_hx_xc as $i=>$one){
//			$store_hx_xc[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count('num');
			$store_hx_xc[$i]['need_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->count();  //2018-7-5修改 lxlx
            $store_hx_xc[$i]['need_hx_num']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=6")->sum('num'); //待核销总份数
		}//end foreach
		//dump($store_hx);
        //农户拼团已核销商品信息
        $store_yhx_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 6 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->select();
		 //dump($store_ds);
        foreach($store_yhx_xc as $i=>$one){
            $store_yhx_xc[$i]['already_hx']=$shop_spellgroup_order->where("c_id=".$one['copy_id']." and state=2")->count();
        }//end foreach

        $this->assign('store_all_xc',$store_all_xc);
        $this->assign('store_ds_xc',$store_ds_xc);
        $this->assign('store_tz_xc',$store_tz_xc);
        $this->assign('store_hx_xc',$store_hx_xc);
        $this->assign('store_yhx_xc',$store_yhx_xc);
		//乡村拼团订单管理---------------------------------------------end
        $this->display('order-sj');
    }//end fun index
	//订单详情
	public function info(){
//        dump(2);
		$member=D('member');
		$shop_spellgroup=D('shop_spellgroup');
		$shop_spellgroup_copy=D('shop_spellgroup_copy');
		$shop_spellgroup_order=D('shop_spellgroup_order');
		$copyinfo=$shop_spellgroup_copy->where('id='.$_GET['id'])->find();
		//副本状态为0，待成功的拼团
		//查找产品信息
		$getsg=$shop_spellgroup->where("id=".$copyinfo['sg_id'])->find();
		$img=explode(',',$getsg['h_imgs']);
		$copyinfo['h_imgs']=$img[0];
		$copyinfo['title']=$getsg['title'];
		$copyinfo['u_num']=$getsg['u_num'];
		$copyinfo['unit']=getunit($getsg['unit']);
		$copyinfo['have_num']=$copyinfo['num'];//已经订购
		$copyinfo['need_num']=$getsg['min_num']-$copyinfo['num'];//还差几分成团
		$copyinfo['copy_state']=$copyinfo['state'];//副本状态
		//已订购人员查询
		$allmem=$shop_spellgroup_order->where("state>=1 and state<>8 and c_id=".$_GET['id'])->select();
		$copyinfo['hold_hx']=0;//等待核销
		$copyinfo['do_hx']=0;//已经核销
		$copyinfo['hold_hx_num']=0;//已经核销份数
		foreach($allmem as $i=>$one){
			$userinfo=$member->where("id=".$one['u_id'])->find();
			//头像 家乡
			$allmem[$i]['avatar']=$userinfo['avatar'];
			$allmem[$i]['hometown']=$userinfo['hometown'];
			if($one['state']==6)$copyinfo['hold_hx_num']=$copyinfo['hold_hx_num']+$one['num'];
			if($one['state']==6)$copyinfo['hold_hx']++;
			if($one['state']==2)$copyinfo['do_hx']++;
		}//end foreach
		
        $this->assign('allmem',$allmem);
        $this->assign('copyinfo',$copyinfo);//dump($copyinfo);
		$this->display('orderdetail-sj');
	}//end fun
	//一键成团
	public function do_succ(){
		//该产品下架
		$sg_id=M('shop_spellgroup_copy')->where('id='.$_GET['id'])->getField('sg_id');
		M('shop_spellgroup')->where("id=".$sg_id)->setField('state',4);//产品下架
		
		$shop_spellgroup_copy=D('shop_spellgroup_copy');
		$data['orsuccess']=1;
		$data['state']=3;//成团待通知
		$shop_spellgroup_copy-> where('id='.$_GET['id'])->setField($data);
		$shop_spellgroup_order=D('shop_spellgroup_order');
		$getmem=$shop_spellgroup_order->where("state=1 and c_id=".$_GET['id'])->setField('state',4);//订单为拼成功
	}//end fun
	//发送到货通知
	public function do_notice(){
		$shop_spellgroup_copy=D('shop_spellgroup_copy');
		$data['orsuccess']=1;
		$data['state']=4;//已经通知
		$shop_spellgroup_copy-> where('id='.$_GET['id'])->setField($data);
		$copy_info = $shop_spellgroup_copy-> where('id='.$_GET['id'])->field('sg_id,c_id')->find();//副本信息
		$goods_info = M("shop_spellgroup")->where("id = {$copy_info['sg_id']}")->find();//商品信息
		$store_info = M("shop_store")->where("c_id = {$copy_info['c_id']}")->find();//店铺信息
		//给所有该副本下的用户发消息
		$shop_spellgroup_order=D('shop_spellgroup_order');
		$voice=D('voice');
		$getmem=$shop_spellgroup_order->where("state=4 and c_id=".$_GET['id'])->select();
		foreach($getmem as $one){
			$shop_spellgroup_order->where("id=".$one['id'])->setField('state',6);//用户待核销
            D("Wx/SendMsg")->arrivalmsg($one['u_id'],$goods_info['title'],$one['receive'],$store_info['address'].$store_info['storename'],$store_info['tel'],"http://www.xiangtws.com/index.php/Wx/MyGrouporder/orderdetail/id/".$one['id']);//消息推送
			$new['u_id']=$one['u_id'];
			$new['content']="有新的订单需要自提！";
			$new['status']=0;
			$new['sendtime']=time();
			$voice->add($new);
		}//end foreach
	}//end fun
}//end class